<template>
  <li>
    <img :src="img" alt="" @click="dialogVisible=true">
    <span class="iconfont recommend" v-show="recommend">&#xe60b;</span>
    <div class="bar">
      <h3>{{name}}</h3>
      <p>
        <span class="rmb">RMB</span>
        <span class="price">{{foodPrice}}/份</span>
        <button @click="dialogFormVisible=true;addDish"><span class="iconfont">&#xe603;</span></button>
      </p>
    </div>
    <!-- 点餐的组件 -->
    <el-dialog title="" :visible.sync="dialogFormVisible" :show-close="false" width="31%" top="25vh" :append-to-body="true">
      <div class="top" slot="title">
        <span class="title">{{name}}</span>
        <span class="el-icon-close close" @click="dialogFormVisible=false"></span>
      </div>
      <div class="mid">
        <div class="food_price">
          <p>￥{{foodPrice}}</p>
          <p>
            <el-rate
              v-model="rate"
              disabled
              text-color="#ff8400"
              score-template="{rate}">
            </el-rate>
          </p>
        </div>
        <div class="count">
          <button class="iconfont" @click="count<=1?count:count--">&#xe605;</button><span>{{count}}</span><button class="iconfont" @click="count++">&#xe603;</button>
        </div>
      </div>
      <textarea name="" id="" cols="30" rows="10" style="width: 544px;hight: 140px" v-model="remarks" placeholder="请输入备注"></textarea>
      <div class="bottom" slot="footer">
        <button class="cancle" @click="dialogFormVisible=false">取消</button>
        <button class="determine" @click="submit">确定</button>
      </div>
    </el-dialog>
    <!-- 查看详情的组件 -->
    <el-dialog :visible.sync="dialogVisible" class="my-dialog" :append-to-body="true" width="45%" top="20vh" :show-close="false">
      <div slot="title" class="head">
        <span class="el-icon-close" @click="dialogVisible=false"></span>
      </div>
      <div class="dialog_body">
        <img :src="img" alt="" class="previewImg">
        <div class="detail">
          <p class="title">{{name}}</p>
          <p class="introduce">{{introduce}}</p>
          <span class="rate">
            推荐指数:&nbsp;&nbsp;
            <el-rate
              v-model="rate"
              disabled
              text-color="#ff8400"
              score-template="{rate}">
            </el-rate>
          </span>
          <span class="price_rmb">价格:&nbsp;&nbsp;<i>RMB</i><strong>{{foodPrice}}</strong></span>
        </div>
      </div>
    </el-dialog>
  </li>
</template>

<script>
import { getSeatInfoAPI } from '@/api/getSeatInfoAPI.js'
import { setOrderDetailAPI } from '@/api/setOrderDetailAPI.js'
import { getOrderDetailAPI } from '@/api/getOrderDetailAPI.js'
export default {
  name: 'foods',
  props: ['name', 'foodPrice', 'introduce', 'img', 'recommend', 'rate'],
  data () {
    return {
      dialogFormVisible: false,
      dialogVisible: false,
      count: 1,
      remarks: '',
      table_num: '',
      avltimer: null
    }
  },
  methods: {
    orderSuccess () {
      this.$message({
        message: '点餐成功',
        type: 'success'
      })
    },
    error () {
      this.$message.error('请先选择桌子')
    },
    async submit () {
      if (this.table_num === '') return this.error()
      this.dialogFormVisible = false
      this.orderSuccess()
      const token = localStorage.getItem('token')
      const data = {
        table_num: this.table_num,
        dish_name: this.name,
        dish_img: this.img,
        unit_price: this.foodPrice,
        dish_count: this.count,
        remarks: this.remarks
      }
      await setOrderDetailAPI(token, data)
    },
    async getSeatInfo () {
      const token = localStorage.getItem('token')
      const { data: res } = await getSeatInfoAPI(token)
      const currentTable = res.data.find(item => item.is_selected === 1)
      if (!currentTable) return
      this.table_num = currentTable.seat_id
    },
    async getOrderDetail () {
      const token = localStorage.getItem('token')
      const { data: res } = await getOrderDetailAPI(token)
      const currentDish = res.data.find(item => item.table_num === this.table_num && item.dish_name === this.name)
      if (!currentDish) return
      this.count = currentDish.dish_count
      this.remarks = currentDish.remarks
    }
  },
  created () {
    this.avltimer = setInterval(() => {
      this.getSeatInfo()
      this.getOrderDetail()
    }, 50)
  },
  beforeDestroy () {
    clearInterval(this.avltimer)
    this.avltimer = null
  }
}
</script>

<style scoped lang="less">
.head{
  display: flex;
  justify-content: end;
  span{
    cursor: pointer;
    font-size: 22px;
    color: #fff;
  }
}
.dialog_body {
  display: flex;
  justify-content: space-between;
  .previewImg {
    width: 400px;
    height: 400px;
    cursor: default;
  }
  .detail {
    color: #fff;
    width: 400px;
    .title{
      font-size: 22px;
      font-weight: 700;
    }
    .introduce{
      font-size: 15px;
      line-height: 25px;
      margin-top: 15px;
    }
    .rate{
      display: flex;
      justify-content: start;
      align-items: center;
      font-size: 16px;
      margin: 15px 0 20px;
    }
    .price_rmb{
      i{
        font-style: normal;
        color: #f0e184;
      }
      strong{
        font-size: 24px;
        font-weight: 400;
        color: #f0e184;
      }
    }
  }
}

li{
    display: inline-block;
    position: relative;
    margin: 10px;
    border-radius: 10px;
    overflow: hidden;
}
img{
    width: 350px;
    height: 350px;
    cursor: pointer;
}
.recommend{
    position: absolute;
    left: 5px;
    top: 5px;
    font-size: 50px;
    color: #ff8400;
}
.bar{
    width: 100%;
    height: 40px;
    color: #fff;
    font-size: 16px;
    line-height: 40px;
    box-sizing: border-box;
    padding: 0 10px;
    background: rgba(0, 0, 0, .6);

    display: flex;
    justify-content: space-between;

    position: absolute;
    bottom: 0;
}
.bar p{
    display: flex;
    align-items: center;
}
.rmb{
    font-size: 14px;
}
.price{
    color: #f0e184;
    margin: 0 10px;
}
.bar button{
    width: 20px;
    height: 20px;
    line-height: 20px;
    background: #ff8400;
    color: #fff;
    border-radius: 50%;
}
.bar button span{
    font-size: 14px;
}

.top {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title {
      font-size: 20px;
    }
    .close {
      color: rgb(136, 136, 136);
      font-size: 20px;
      cursor: pointer;
    }
    .close:hover {
      color: #333;
    }
  }
  .mid {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: -25px;
    .food_price {
      p {
        color: #ff9900;
        font-weight: 700;
        font-size: 18px;
      }
    }
    .count {
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        display: block;
        width: 20px;
        height: 18px;
        border: 1px solid #ff8400;
        line-height: 15px;
        text-align: center;
        font-size: 12px;
        color: rgb(133, 133, 133);
      }
      button {
        width: 20px;
        height: 20px;
        border: 1px solid #ff8400;
        background-color: #ff8400;
        color: #fff;
        line-height: 17px;
        text-align: center;
        font-size: 12px;
      }
    }
  }
  textarea {
    font-size: 14px;
    outline: none;
    resize: none;
    border-color: #ff8400;
    margin-top: 10px;
    padding: 5px;
    color: rgb(119, 118, 118);
  }
  textarea::placeholder{
    color: rgb(184, 184, 184);
  }
  .bottom {
    display: flex;
    justify-content: end;
    align-items: center;
    margin-top: -25px;
    .cancle {
      width: 55px;
      height: 30px;
      border: 1px solid rgb(201, 199, 199);
      color: rgb(170, 170, 170);
      line-height: 20px;
      text-align: center;
      margin-right: 10px;
      border-radius: 2px;
    }
    .cancle:hover {
      background-color: rgb(248, 248, 248);
    }
    .determine {
      width: 56px;
      height: 31px;
      color: #fff;
      background-color: #ff8400;
      line-height: 20px;
      text-align: center;
      border-radius: 2px;
    }
    .determine:hover {
      background-color: #ff9900;
    }
  }
</style>
<style>
.my-dialog .el-dialog{
    background: rgba(0,0,0,0);
    position: relative;
    margin: 0 auto 50px;
    box-shadow: none;
    border-radius: 2px;
    box-sizing: border-box;
    width: 50%;
}
</style>
